package com.kwm.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kwm.Mapper.EmpExprMapper;
import com.kwm.Mapper.EmpMapper;
import com.kwm.Service.EmpService;
import com.kwm.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceimpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Override
    public PageBean page(EmpQueryParam param) {
    //1. 设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());
    //2. 执行查询
        List<Emp> empList = empMapper.list(param);
   //3. 解析封装分页结果
        Page<Emp> p = (Page<Emp>) empList;
        return new PageBean(p.getTotal(), p.getResult());
    }



    @Transactional
    @Override
    public void add(Emp emp) {
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.add(emp);

        Integer id = emp.getId();
        log.info("id = {}", id);


        List<EmpExpr> exprList = emp.getExprList();
        //关联员工id
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach((expr) ->
                expr.setEmpId(id));
            log.info("工作经历为：{}", exprList);
            empExprMapper.insertBach(exprList);
        }
    }

    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        //删除员工
        empMapper.delete(ids);
        //删除员工经历
        empExprMapper.exprdelete(ids);

    }

    @Override
    public Emp getId(Integer id) {
        Emp emp = empMapper.getId(id);
        log.info("查询结果为：{}",emp);
        return emp;

    }
    @Transactional
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.update(emp);
        System.out.println("emp的id为 = " + emp.getId());
        //2.根据员工的id先删除员工的工作经历信息
        Integer id = emp.getId();
        empExprMapper.deleteEmpExpr(id);
        // 新增员工的工作经历数据


        System.out.println("删除员工工作经历的id为 = " + id);
        List<EmpExpr> List_e = emp.getExprList();
        if(!CollectionUtils.isEmpty(List_e)){
            List_e.forEach((e)-> e.setEmpId(id));
            empExprMapper.insertBach(List_e);
        }

    }

    @Override
    public LoginEmp Login(Emp emp) {

        Emp emp1 = empMapper.selectUsernameAndPassword(emp);

        if (emp1 != null){

            Map<String, Object> datamap = new HashMap<>();
            datamap.put("id",emp1.getId());
            datamap.put("username",emp1.getUsername());
            String jwt = JwtUtils.generateJwt(datamap);

            return new LoginEmp(emp1.getId(),emp1.getUsername(),emp1.getName(),jwt);
        }

        return null;
    }
}

